home *** CD-ROM | disk | FTP | other *** search
/ Precision Software Appli…tions Silver Collection 1 / Precision Software Applications Silver Collection Volume One (PSM) (1993).iso / windows / games / tricho.exe / TRICHO.C < prev    next >
C/C++ Source or Header  |  1990-03-16  |  7KB  |  320 lines

  1. /*  tricho.c        program that draws fractal inside an equilaterial
  2.  *            triangle as a result of an apparently random process
  3.  *            as shown on P.B.S. television show NOVA "THE STRANGE
  4.  *            NEW SCIENCE OF CHAOS".
  5.  *             Marty Belles 9-2-89.
  6.  */
  7.  
  8. #include <windows.h>
  9. #include <stdlib.h>
  10. #include "tricho.h"
  11.  
  12. long FAR PASCAL WndProc (HWND, unsigned, WORD, LONG);
  13. void DrawTriangle (HWND);
  14. void Newplot (short, short, LONG, HWND);
  15. int Genrand();
  16. short nRed, nGreen, nBlue;
  17. short nFlag = 0;
  18.  
  19. short xClient, yClient, x, y;
  20. char szAppName [] = "tricho";
  21.  
  22. int PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  23. HANDLE        hInstance, hPrevInstance;
  24. LPSTR        lpszCmdLine;
  25. int        nCmdShow;
  26. {
  27. HWND        hWnd;
  28. MSG        msg;
  29. WNDCLASS    wndclass;
  30.  
  31.     if (!hPrevInstance) {
  32.     wndclass.style        = CS_HREDRAW | CS_VREDRAW;
  33.     wndclass.lpfnWndProc    = WndProc;
  34.     wndclass.cbClsExtra    = 0;
  35.     wndclass.cbWndExtra    = 0;
  36.     wndclass.hInstance    = hInstance;
  37.     wndclass.hIcon        = NULL;
  38.     wndclass.hCursor    = LoadCursor (NULL, IDC_ARROW);
  39.     wndclass.hbrBackground    = GetStockObject (WHITE_BRUSH);
  40.     wndclass.lpszMenuName    = szAppName;
  41.     wndclass.lpszClassName    = szAppName;
  42.  
  43.     if (!RegisterClass (&wndclass))
  44.         return FALSE;
  45.     }
  46.  
  47.     hWnd = CreateWindow (szAppName,        /*    window class name    */
  48.             "Triangle Chaos",        /*    window caption        */
  49.             WS_OVERLAPPEDWINDOW,    /*    window style        */
  50.             CW_USEDEFAULT,        /*    initial x position    */
  51.             0,                /*    initial y position    */
  52.             CW_USEDEFAULT,        /*    initial x size        */
  53.             0,                /*    initial y size        */
  54.             NULL,            /*    parent window handle    */
  55.             NULL,            /*    window menu handle    */
  56.             hInstance,            /*    program instance handle */
  57.             NULL);            /*    create parameters    */
  58.  
  59.     ShowWindow (hWnd, nCmdShow);
  60.  
  61.     UpdateWindow (hWnd);
  62.  
  63.     while (TRUE) {
  64.     if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) {
  65.         if (msg.message == WM_QUIT)
  66.         break;
  67.  
  68.         TranslateMessage (&msg);
  69.         DispatchMessage (&msg);
  70.         }
  71.     else
  72.         DrawTriangle (hWnd);
  73.     }
  74.     return msg.wParam;
  75. }
  76.  
  77. void DrawTriangle (hWnd)
  78. HWND    hWnd;
  79. {
  80. int i;
  81.  
  82.     i = Genrand();            /*    dice throw  */
  83.     switch(i) {
  84.     case 1:
  85.     case 2:
  86.         switch (nFlag) {
  87.         case 0:
  88.             Newplot(xClient / 2, 0, RGB(nRed, nGreen, nBlue), hWnd);
  89.             break;
  90.  
  91.         case 1:     /*    red  */
  92.             Newplot(xClient / 2, 0, RGB(255, 0, 0), hWnd);
  93.             break;
  94.  
  95.         case 2:     /*    random    */
  96.             nRed   = rand () % 255;
  97.             nGreen = rand () % 255;
  98.             nBlue  = rand () % 255;
  99.             NewPlot (xClient / 2, 0, RGB(nRed, nGreen, nBlue), hWnd);
  100.             break;
  101.          }
  102.         break;
  103.  
  104.     case 3:
  105.     case 4:
  106.         switch (nFlag) {
  107.         case 0:     /*    blackonwhite or white on black    */
  108.             Newplot(0, yClient, RGB(nRed, nGreen, nBlue), hWnd);
  109.             break;
  110.  
  111.         case 1:     /*    white  */
  112.             Newplot(0, yClient, RGB(255, 255, 255), hWnd);
  113.             break;
  114.  
  115.         case 2:     /*    random    */
  116.             nRed   = rand () % 255;
  117.             nGreen = rand () % 255;
  118.             nBlue  = rand () % 255;
  119.             NewPlot (0, yClient, RGB(nRed, nGreen, nBlue), hWnd);
  120.             break;
  121.          }
  122.         break;
  123.  
  124.     case 5:
  125.     case 6:
  126.         switch (nFlag) {
  127.         case 0:     /*    blackonwhite or white on black    */
  128.             Newplot(xClient, yClient, RGB(nRed, nGreen, nBlue), hWnd);
  129.             break;
  130.  
  131.         case 1:     /*    blue  */
  132.             Newplot(xClient, yClient, RGB(0, 0, 255), hWnd);
  133.             break;
  134.  
  135.         case 2:     /*    random    */
  136.             nRed   = rand () % 255;
  137.             nGreen = rand () % 255;
  138.             nBlue  = rand () % 255;
  139.             NewPlot (xClient, yClient, RGB(nRed, nGreen, nBlue), hWnd);
  140.             break;
  141.          }
  142.         break;
  143.  
  144.     }
  145. }
  146.  
  147. void Newplot(vx, vy, lColor, hWnd)
  148. HWND    hWnd;
  149. long    lColor;
  150. short vx, vy;
  151. {
  152. int i, newx, newy;
  153. HDC hDC;
  154.  
  155.     hDC = GetDC(hWnd);
  156.     /*    calculate new x coordinate  */
  157.     i = abs(vx - x) / 2;
  158.     if (vx > x)
  159.     newx = i + x;
  160.     else
  161.     newx = i + vx;
  162.  
  163.     /*    calculate new y coordinate  */
  164.     i = abs(vy - y) / 2;
  165.     if (vy > y)
  166.     newy = i + y;
  167.     else
  168.     newy = i + vy;
  169.  
  170.     /*    plot new coordinates  */
  171.  
  172.     SetPixel (hDC, newx, newy, lColor);
  173.  
  174.     ReleaseDC (hWnd, hDC);
  175.  
  176.     /*    update global variables  */
  177.     x = newx;
  178.     y = newy;
  179.  
  180. }
  181.  
  182. int Genrand()
  183. {
  184. int i;
  185.     i = rand();
  186.  
  187.     if(i <= 5461)
  188.     return 1;
  189.     if ((i > 5461) && (i <= 10922))
  190.     return 2;
  191.     if ((i > 10922) && (i <= 16383))
  192.     return 3;
  193.     if ((i > 16383) && (i <= 21844))
  194.     return 4;
  195.     if ((i > 21844) && (i <= 27305))
  196.     return 5;
  197.     else
  198.     return 6;
  199.  
  200. }
  201.  
  202. BOOL FAR PASCAL AboutDlgProc (hDlg, iMessage, wParam, lParam)
  203. HWND        hDlg;
  204. unsigned    iMessage;
  205. WORD        wParam;
  206. LONG        lParam;
  207. {
  208. switch (iMessage) {
  209.     case WM_INITDIALOG:
  210.     break;
  211.  
  212.     case WM_COMMAND:
  213.     switch (wParam) {
  214.         case IDOK:
  215.         EndDialog (hDlg, 0);
  216.         break;
  217.  
  218.         default:
  219.         return FALSE;
  220.         }
  221.     break;
  222.  
  223.     default:
  224.     return FALSE;
  225.  
  226.     }
  227. return TRUE;
  228. }
  229.  
  230.  
  231. long FAR PASCAL WndProc (hWnd, iMessage, wParam, lParam)
  232. HWND        hWnd;
  233. unsigned    iMessage;
  234. WORD        wParam;
  235. LONG        lParam;
  236. {
  237. HDC        hDC;
  238. static FARPROC    lpfnAboutDlgProc;
  239. static HWND    hInstance;
  240.  
  241.     switch (iMessage) {
  242.     case WM_CREATE:
  243.         hInstance = ((LPCREATESTRUCT) lParam)->hInstance;
  244.         lpfnAboutDlgProc = MakeProcInstance (AboutDlgProc, hInstance);
  245.         break;
  246.  
  247.     case WM_SIZE:
  248.         xClient = LOWORD (lParam);
  249.         yClient = HIWORD (lParam);
  250.  
  251.         /*    plot initial vertices of triangle  */
  252.         hDC = GetDC(hWnd);
  253.         SetPixel (hDC, xClient / 2, 0, 0L);     /*    center top  */
  254.         SetPixel (hDC, 0, yClient, 0L);        /*    bottom left  */
  255.         SetPixel (hDC, xClient, yClient, 0L);   /*    bottom right  */
  256.         ReleaseDC (hWnd, hDC);
  257.  
  258.         /* initialize starting point (arbitrary)  */
  259.         x = xClient;
  260.         y = yClient;
  261.  
  262.         break;
  263.  
  264.     case WM_COMMAND:
  265.         switch (wParam) {
  266.         case IDM_BLACKONWHITE:
  267.             nFlag  = 0;
  268.             nRed   = 0;
  269.             nGreen = 0;
  270.             nBlue  = 0;
  271.             SetClassWord (hWnd, GCW_HBRBACKGROUND,
  272.                   GetStockObject (WHITE_BRUSH));
  273.             InvalidateRect (hWnd, NULL, TRUE);
  274.             break;
  275.  
  276.         case IDM_WHITEONBLACK:
  277.             nFlag  = 0;
  278.             nRed   = 255;
  279.             nGreen = 255;
  280.             nBlue  = 255;
  281.             SetClassWord (hWnd, GCW_HBRBACKGROUND,
  282.                     GetStockObject (BLACK_BRUSH));
  283.             InvalidateRect (hWnd, NULL, TRUE);
  284.             break;
  285.  
  286.         case IDM_REDWHITEBLUE:
  287.             nFlag = 1;
  288.             SetClassWord (hWnd, GCW_HBRBACKGROUND,
  289.                     GetStockObject (BLACK_BRUSH));
  290.             InvalidateRect (hWnd, NULL, TRUE);
  291.             break;
  292.  
  293.         case IDM_RANDOM:
  294.             nFlag = 2;
  295.             SetClassWord (hWnd, GCW_HBRBACKGROUND,
  296.                     GetStockObject (BLACK_BRUSH));
  297.             InvalidateRect (hWnd, NULL, TRUE);
  298.             break;
  299.  
  300.         case IDM_ABOUT:
  301.             DialogBox (hInstance, "TrichoAbout", hWnd,
  302.                     lpfnAboutDlgProc);
  303.             break;
  304.  
  305.         default:
  306.             break;
  307.         }
  308.         break;
  309.  
  310.     case WM_DESTROY:
  311.         PostQuitMessage (0);
  312.         break;
  313.  
  314.     default:
  315.         return DefWindowProc (hWnd, iMessage, wParam, lParam);
  316.     }
  317.     return 0L;
  318.  
  319. }
  320.